package cn.didi.feidi.androidorder.fragment.personal;


import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.activity.AppSettingActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonBoxMoneyActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonLoginActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonMessageActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonReceiveAddressActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonWalletActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonWalletPayPasswordFirstSetActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonalCollectionsActivity;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.assembly.FeidiActionBar;
import cn.didi.feidi.androidorder.fragment.FeiDiMainBaseFragment;
import cn.didi.feidi.androidorder.manager.UserManager;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class PersonCenterFragment extends FeiDiMainBaseFragment {

    private static final String TAG = "PersonCenterFragment";
    private LinearLayout personMessage;
    private LinearLayout deliveryAddress;
    private LinearLayout personDeposit;
    private LinearLayout personWallet;
    private LinearLayout personCollection;
    private LinearLayout appSetting;
    private TextView personName;
    private CircleImageView personIcon;


    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonCenterFragment newInstance(Bundle bundle) {
        PersonCenterFragment fragment = new PersonCenterFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_person_center, container, false);
        Log.d(TAG, "initFragment");
        return rootView;
    }


    @Override
    protected void initViews(View rootView) {
        personName = (TextView) rootView.findViewById(R.id.person_center_person_name_tv);
        personIcon = (CircleImageView) rootView.findViewById(R.id.person_center_person_icon);

        personMessage = (LinearLayout) rootView.findViewById(R.id.person_center_person_message);
        deliveryAddress = (LinearLayout) rootView.findViewById(R.id.person_center_delivery_address);
        personDeposit = (LinearLayout) rootView.findViewById(R.id.person_center_person_deposit);
        personWallet = (LinearLayout) rootView.findViewById(R.id.person_center_person_wallet);
        personCollection = (LinearLayout) rootView.findViewById(R.id.person_center_person_collection);
        appSetting = (LinearLayout) rootView.findViewById(R.id.person_center_app_setting);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserManager.isBingAccount()) {
            personName.setBackgroundColor(Color.argb(0, 0, 0, 0));
            personName.setText(UserManager.getName());
            String headPortrait = UserManager.getHeadPortrait();

            Glide.with(mActivity).load(headPortrait).asBitmap().placeholder(R.drawable.person_center_person_default_icon).centerCrop().into(new BitmapImageViewTarget(personIcon) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    personIcon.setImageDrawable(circularBitmapDrawable);
                }
            });

        } else {
            Log.e(TAG, "initData: 用户未登录注册");
            personName.setText("立即登录");
            personName.setBackgroundResource(R.drawable.bg_corner_person_center_login);
        }
    }

    @Override
    protected void addListener() {
        personMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManager.isBingAccount()) {
                    return;
                }
                Intent intent = new Intent(getContext(), PersonMessageActivity.class);
                startActivity(intent);
            }
        });

        personName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toLoginOrToPersonDetail();
            }
        });

        personIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toLoginOrToPersonDetail();
            }
        });

        deliveryAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManager.isBingAccount()) {
                    Toast.makeText(mActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(getContext(), PersonReceiveAddressActivity.class);
                startActivity(intent);
            }
        });
        personDeposit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManager.isBingAccount()) {
                    Toast.makeText(mActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(getContext(), PersonBoxMoneyActivity.class);
                startActivity(intent);
            }
        });
        personWallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManager.isBingAccount()) {
                    Toast.makeText(mActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!UserManager.isSetPayPassword()){//未设置支付密码
                    new CommomDialog(getContext(), R.style.dialog, "您尚未设置支付密码", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            if (confirm) {
                                makeToast("现在设置");
                                Intent intent = new Intent(getContext(), PersonWalletPayPasswordFirstSetActivity.class);
                                getContext().startActivity(intent);
                                dialog.dismiss();
                            } else {
                                dialog.dismiss();
                            }
                        }
                    }, false, true).setNegativeButton("返回").setPositiveButton("现在设置").show();
                }else {
                    Intent intent = new Intent(getContext(), PersonWalletActivity.class);
                    startActivity(intent);
                }
            }
        });
        personCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!UserManager.isBingAccount()) {
                    Toast.makeText(mActivity, "请先登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(getActivity(), PersonalCollectionsActivity.class);
                startActivity(intent);
            }
        });
        appSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), AppSettingActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void actionBarSetting(FeidiActionBar view) {
        view.setVisibility(View.GONE);
    }

    private void toLoginOrToPersonDetail() {
        if (UserManager.isBingAccount()) {
            Intent intent = new Intent(getContext(), PersonMessageActivity.class);
            startActivity(intent);
        } else {
            Intent intent = new Intent(getContext(), PersonLoginActivity.class);
            startActivity(intent);
        }
    }
}
